package com.itheima.consumer.listener;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
@Slf4j
public class SpringRabbitListener {
    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(queues = "hmallDemo1")
    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
        System.out.println("spring 消费者接收到消息：【" + msg + "】");
    }

    @RabbitListener(queues = "hmallDemo2")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "hmallDemo2")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenfanoutqueue1(String msg) throws InterruptedException {
        System.err.println("消费者1........接收到消息：【" + msg + "】" + LocalTime.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenfanoutqueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
    }

    //    @RabbitListener(queues = "direct.queue1")
    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue1")
            , exchange = @Exchange(value = "hamll.dicect", type = ExchangeTypes.DIRECT)
            , key = {"blue", "red"}
    ))
    public void listendirectqueue1(String msg) throws InterruptedException {
        System.err.println("消费者1........接收到消息：【" + msg + "】" + LocalTime.now());
    }

    //    @RabbitListener(queues = "direct.queue2")
    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue2")
            , exchange = @Exchange(value = "hamll.dicect", type = ExchangeTypes.DIRECT)
            , key = {"yellow", "red"}
    ))
    public void listendirectqueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
    }

    //    @RabbitListener(queues = "topic.queue1")
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue1")
            , exchange = @Exchange(value = "hamll.topic", type = ExchangeTypes.TOPIC)
            , key = "china.#"
    ))
    public void listentopicqueue1(String msg) throws InterruptedException {
        System.err.println("消费者1........接收到消息：【" + msg + "】" + LocalTime.now());
    }

    //    @RabbitListener(queues = "topic.queue2")
    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue2")
            , exchange = @Exchange(value = "hamll.topic", type = ExchangeTypes.TOPIC)
            , key = "#.news"
    ))
    public void listentopicqueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UserDTO{
        private String name;
        private Integer age;
    }
    @RabbitListener(queues = "object.queue")
    public void onObjectMessage(UserDTO user){
        log.info("user：{}", user);
    }

}